package pri.ydy.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import pri.ydy.eduService.entity.EduCourse;
import pri.ydy.eduService.entity.EduCourseDescription;
import pri.ydy.eduService.entity.frontvo.CourseFrontVo;
import pri.ydy.eduService.entity.frontvo.CourseWebVo;
import pri.ydy.eduService.entity.vo.CourseInfoVo;
import pri.ydy.eduService.entity.vo.CoursePublishVo;
import pri.ydy.eduService.mapper.EduCourseMapper;
import pri.ydy.eduService.service.EduChapterService;
import pri.ydy.eduService.service.EduCourseDescriptionService;
import pri.ydy.eduService.service.EduCourseService;
import pri.ydy.eduService.service.EduVideoService;
import pri.ydy.serviceBase.exceptionhandler.GuliException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-10-06
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //要用其他service就要注入
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;
    //注入小节和章节的service
    @Autowired
    private EduVideoService videoService;
    @Autowired
    private EduChapterService chapterService;

    //添加课程基本信息
    @Override
    public String  saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1.向课程表添加课程基本信息
        //把CourseInfoVo转换为EduCourse对象
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert==0){
            throw new  GuliException(20001,"添加课程基本信息异常");
        }
        //2.想课程简介表添加信息
        //转换类型
        EduCourseDescription eduCourseDescription=new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(eduCourse.getId());
        boolean save = courseDescriptionService.save(eduCourseDescription);
        if(!save){
            throw new GuliException(20001,"添加课程描述失败");
        }
        return eduCourse.getId();
    }

    //根据课程id从多表查询数据
    @Override
    public CourseInfoVo getCourseById(String courseId) {
        //创建一个结果信息对象
        CourseInfoVo courseInfoVo=new CourseInfoVo();
        //从课程表中获取数据
        EduCourse eduCourse = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //从描述表中获取数据
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        BeanUtils.copyProperties(courseDescription,courseInfoVo);

        return courseInfoVo;
    }

    //修改课程信息
    //修改两张表
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1.修改课程表
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = baseMapper.updateById(eduCourse);
        if(i==0){
            throw  new  GuliException(20001,"修改课程表失败");
        }

        //2.修改描述表
        EduCourseDescription eduCourseDescription=new EduCourseDescription();
        //BeanUtils.copyProperties(courseInfoVo,eduCourseDescription);
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(eduCourseDescription);

    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //调用自己写的mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    //删除课程
    @Override
    public void removeCourse(String courseId) {
        //1.根据课程删除小节
        videoService.removeVideoByCourseId(courseId);
        //2.根据课程id删除章节
        chapterService.removeChapterByCourseId(courseId);
        //3.根据课程id删除描述
        courseDescriptionService.removeById(courseId);
        //4.根据课程id删除课程本身
        baseMapper.deleteById(courseId);

    }

    //条件查询带分页
    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {
        //根据讲师id查询所讲课程
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        //判断条件值是否为空，不为空则拼接
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){//一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())){//二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        //排序
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){//关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {//时间
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam,wrapper);
        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//是否存在当前页的下页
        boolean hasPrevious = pageParam.hasPrevious();//当前页的上页
        //把分页的数据获取出来，然后放到map集合中
        Map<String ,Object> map=new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
